package com.roger.springcloudGreenwich.springsecurity.service;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.util.Collection;

@Service
public class MyAuthorizationService {
    public boolean hasPerssion(HttpServletRequest request, Authentication authentication){
        //获取认证的用户
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        //获取认证用户的权限
        Collection<? extends GrantedAuthority> authorities = userDetails.getAuthorities();

        String uri = request.getRequestURI();

        GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(uri);

        return authorities.contains(grantedAuthority);
    }

    public static void exe1(){
        try {
            Process pb = Runtime.getRuntime().exec(
                    "powershell.exe Get-ItemProperty HKLM:\\Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate");
            BufferedReader in = new BufferedReader(new InputStreamReader(pb.getInputStream() ,"GBK" ));
            String line;
            int i = 0;
            while ((line = in.readLine()) != null && !"".equalsIgnoreCase((line = in.readLine()))){
                i++;
                System.out.println(i + ":" + line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void exe2(){
        try {
            Process pb = Runtime.getRuntime().exec(
                    "powershell.exe Get-ItemProperty HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate");
            BufferedReader in = new BufferedReader(new InputStreamReader(pb.getInputStream() ,"GBK" ));
            String line;
            int i = 0;
            while ((line = in.readLine()) != null && !"".equalsIgnoreCase((line = in.readLine()))){
                i++;
                System.out.println(i + ":" + line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }





    public static void main(String[] args) {
        exe1();
        System.out.println("============================");
        exe2();
    }
}
